package cn.com.xxlibrary.tagLib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import cn.com.xxlibrary.util.UtilTools;

public class PagingExt extends TagSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int pageNo;
	private int pageSize;
	private int count;
	private String url;
	
	private String op;
	private String wd;
	private String st;

	@Override
	public int doEndTag() throws JspException {
		StringBuilder strTag = new StringBuilder();
		strTag.append("<div class='pagination'>");
		if (this.count == 0) {
			strTag.append("<b>没有可显示的项目</b>");
		} else {
			strTag.append("<form method='get' action='");
			strTag.append(this.url);
			strTag.append("' name='qPagerForm'>");

			strTag.append("<input type='hidden' name='pageNo' value='");
			strTag.append(this.pageNo);
			strTag.append("' /><input type='hidden' name='pageSize' value='");
			strTag.append(this.pageSize);
			strTag.append("' />");
			
			if(UtilTools.isNotNullorEmpty(op)){
				strTag.append("<input type='hidden' name='op' value='");
				strTag.append(this.op);
				strTag.append("' />");
			}

			if(UtilTools.isNotNullorEmpty(wd)){
				strTag.append("<input type='hidden' name='wd' value='");
				strTag.append(this.wd);
				strTag.append("' />");
			}

			if(UtilTools.isNotNullorEmpty(st)){
				strTag.append("<input type='hidden' name='st' value='");
				strTag.append(this.st);
				strTag.append("' />");
			}

			strTag.append("&nbsp;共<b>");
			strTag.append(this.count);
			strTag.append("</b>项,<b>");
			int pageCount = (int) Math.ceil(1.0 * this.count / this.pageSize);
			strTag.append(pageCount);
			strTag.append("</b>页&nbsp;");

			if (this.pageNo > pageCount) {
				this.pageNo = pageCount;
			}
			if (this.pageNo < 1) {
				this.pageNo = 1;
			}
			if (this.pageNo == 1) {
				strTag.append("<span class='disabled'>&laquo;&nbsp;上一页</span>");
			} else {
				strTag.append("<a href='javascript:turnOverPage(")
						.append(this.pageNo - 1)
						.append(")'>&laquo;&nbsp;上一页</a>");
			}

			int start = 1;
			if (this.pageNo > 4) {
				start = this.pageNo - 1;
				strTag.append("<a href='javascript:turnOverPage(1)'>1</a>");
				strTag.append("<a href='javascript:turnOverPage(2)'>2</a>");
				strTag.append("&hellip;");
			}
			int end = this.pageNo + 1;
			if (end > pageCount) {
				end = pageCount;
			}
			for (int i = start; i <= end; i++) {
				if (this.pageNo == i) {
					strTag.append("<span class='current'>").append(this.pageNo)
							.append("</span>");
				} else {
					strTag.append("<a href='javascript:turnOverPage(")
							.append(i).append(")'>").append(i).append("</a>");
				}
			}

			if (end < pageCount - 2) {
				strTag.append("&hellip;");
			}
			if (end < pageCount - 1) {
				strTag.append("<a href='javascript:turnOverPage(")
						.append(pageCount - 1).append(")'>")
						.append(pageCount - 1).append("</a>");
			}
			if (end < pageCount) {
				strTag.append("<a href='javascript:turnOverPage(")
						.append(pageCount).append(")'>").append(pageCount)
						.append("</a>");
			}

			if (pageNo == pageCount) {
				strTag.append("<span class='disabled'>下一页&nbsp;&raquo;</span>");
			} else {
				strTag.append("<a href='javascript:turnOverPage(")
						.append(this.pageNo + 1)
						.append(")'>下一页&nbsp;&raquo;</a>");
			}
			strTag.append("</form>");

			strTag.append("<script language='javascript'>");
			strTag.append("function turnOverPage(no){");
			strTag.append("if(no>");
			strTag.append(pageCount);
			strTag.append("){no=");
			strTag.append(pageCount);
			strTag.append(";}");
			strTag.append("if(no<1){no=1;}");
			strTag.append("document.qPagerForm.pageNo.value=no;");
			strTag.append("document.qPagerForm.submit();}");
			strTag.append("</script>");
			strTag.append("</div>");
		}
		try {
			this.pageContext.getOut().write(strTag.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return this.SKIP_BODY;
	}

	@Override
	public int doStartTag() throws JspException {
		// TODO Auto-generated method stub
		return super.doStartTag();
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getOp() {
		return op;
	}

	public void setOp(String op) {
		this.op = op;
	}

	public String getWd() {
		return wd;
	}

	public void setWd(String wd) {
		this.wd = wd;
	}

	public String getSt() {
		return st;
	}

	public void setSt(String st) {
		this.st = st;
	}

}
